/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.panel;

import java.util.ArrayList;
import java.util.List;

import org.rails.extjs.Container;
import org.rails.extjs.Option;
import org.rails.extjs.button.Button;
import org.rails.extjs.toolbar.Toolbar;

public class Panel extends Container {

	public static final String SHADOW_SIDES = "sides";
	public static final String SHADOW_FRAME = "frame";
	public static final String SHADOW_DROP = "drop";

	public static final String BUTTON_ALIGN_RIGHT = "right";
	public static final String BUTTON_ALIGN_LEFT = "left";
	public static final String BUTTON_ALIGN_CENTER = "center";

	protected final List<Button> buttons = new ArrayList<Button>();
	protected final List<Tool> tools = new ArrayList<Tool>();

	public Panel() {
		super();
	}

	public Panel(Option configOptions) {
		super(configOptions);
	}

	public Panel(String title) {
		super();
		setTitle(title);
	}

	public Panel(String id, String title) {
		super(id);
		setTitle(title);
	}

	@Override
	protected void createItems() {
		super.createItems();
		if (buttons.size() > 0)
			addOption("buttons", buttons);
		if (tools.size() > 0)
			addOption("tools", tools);
	}

	public void setTitle(String title) {
		addOption("title", title);
	}

	public String getTitle() {
		return getString("title", "");
	}

	public void setSplit(Boolean split) {
		addOption("split", split);
	}

	public Boolean isSplit() {
		return getBoolean("split", false);
	}

	public void setHtml(String html) {
		addOption("html", html);
	}

	public String getHtml() {
		return getString("html", "");
	}

	public void setCollapsible(Boolean collapsible) {
		addOption("collapsible", collapsible);
	}

	public Boolean isCollapsible() {
		return getBoolean("collapsible", false);
	}

	public void setAnimCollapse(Boolean animCollapse) {
		addOption("animCollapse", animCollapse);
	}

	public Boolean isAnimCollapse() {
		return getBoolean("animCollapse", false);
	}

	public void setAutoLoad(Load autoLoad) {
		addOption("autoLoad", autoLoad);
	}

	public Load getAutoLoad() {
		return (Load) getOption("autoLoad");
	}

	public void setAutoScroll(Boolean autoScroll) {
		addOption("autoScroll", autoScroll);
	}

	public Boolean isAutoScroll() {
		return getBoolean("autoScroll", false);
	}

	public void setBaseClassName(String baseCls) {
		addOption("baseCls", baseCls);
	}

	public String getBaseClassName() {
		return getString("baseCls", "x-panel");
	}

	public void setBbar(Toolbar bbar) {
		addOption("bbar", bbar);
	}

	public Toolbar getBbar() {
		return (Toolbar) getOption("bbar");
	}

	public void setTbar(Toolbar tbar) {
		addOption("tbar", tbar);
	}

	public Toolbar getTbar() {
		return (Toolbar) getOption("tbar");
	}

	public void setFbar(Toolbar fbar) {
		addOption("fbar", fbar);
	}

	public Toolbar getFbar() {
		return (Toolbar) getOption("fbar");
	}

	public void setBodyBorder(Boolean bodyBorder) {
		addOption("bodyBorder", bodyBorder);
	}

	public Boolean isBodyBorder() {
		return getBoolean("bodyBorder", true);
	}

	public void setBodyStyle(String bodyStyle) {
		addOption("bodyStyle", bodyStyle);
	}

	public String getBodyStyle() {
		return getString("bodyStyle", "");
	}

	public void setBorder(Boolean border) {
		addOption("border", border);
	}

	public Boolean isBorder() {
		return getBoolean("border", true);
	}

	public void setButtonAlign(String buttonAlign) {
		addOption("buttonAlign", buttonAlign);
	}

	public String getButtonAlign() {
		return getString("buttonAlign",
				Panel.BUTTON_ALIGN_RIGHT);
	}

	public void addButton(Button button) {
		buttons.add(button);
	}

	public Button getButton(int index) {
		return buttons.get(index);
	}

	public void removeButton(int index) {
		buttons.remove(index);
	}

	public void insertButton(int index, Button button) {
		buttons.add(index, button);
	}

	public void clearButton() {
		buttons.clear();
	}

	public List<Button> getButtons() {
		return buttons;
	}

	public void addTool(Tool tool) {
		tools.add(tool);
	}

	public Tool getTool(int index) {
		return tools.get(index);
	}

	public void removeTool(int index) {
		tools.remove(index);
	}

	public void insertTool(int index, Tool tool) {
		tools.add(index, tool);
	}

	public void clearTools() {
		tools.clear();
	}

	public List<Tool> getTools() {
		return tools;
	}

	public void setClosable(Boolean closable) {
		addOption("closable", closable);
	}

	public Boolean isClosable() {
		return getBoolean("closable", false);
	}

	public void setCollapseFirst(Boolean collapseFirst) {
		addOption("collapseFirst", collapseFirst);
	}

	public Boolean isCollapseFirst() {
		return getBoolean("collapseFirst", true);
	}

	public void setCollapsed(Boolean collapsed) {
		addOption("collapsed", collapsed);
	}

	public Boolean isCollapsed() {
		return getBoolean("collapsed", false);
	}

	public void setCollapsedClassName(String collapsedCls) {
		addOption("collapsedCls", collapsedCls);
	}

	public String getCollapsedClassName() {
		return getString("collapsedCls", "x-panel-collapsed");
	}

	public void setDisabled(Boolean disabled) {
		addOption("disabled", disabled);
	}

	public Boolean isDisabled() {
		return getBoolean("disabled", false);
	}

	public void setHideCollapseTool(Boolean hideCollapseTool) {
		addOption("hideCollapseTool", hideCollapseTool);
	}

	public Boolean getHideCollapseTool() {
		return getBoolean("hideCollapseTool", false);
	}

	public void setIconClassName(String iconCls) {
		addOption("iconCls", iconCls);
	}

	public String getIconClassName() {
		return getString("iconCls", "");
	}

	public void setMaskDisabled(Boolean maskDisabled) {
		addOption("maskDisabled", maskDisabled);
	}

	public Boolean isMaskDisabled() {
		return getBoolean("maskDisabled", true);
	}

	public void setMinButtonWidth(Number minButtonWidth) {
		addOption("minButtonWidth", minButtonWidth);
	}

	public Integer getMinButtonWidth() {
		return getInteger("minButtonWidth", 75);
	}

	public void setPreventBodyReset(Boolean preventBodyReset) {
		addOption("preventBodyReset", preventBodyReset);
	}

	public Boolean isPreventBodyReset() {
		return getBoolean("preventBodyReset", false);
	}

	public void setShadow(String shadow) {
		addOption("shadow", shadow);
	}

	public String getShadow() {
		return getString("shadow", SHADOW_SIDES);
	}

	public void setShadowOffset(Integer shadowOffset) {
		addOption("shadowOffset", shadowOffset);
	}

	public Integer getShadowOffset() {
		return getInteger("shadowOffset", 4);
	}

	public void setShim(Boolean shim) {
		addOption("shim", shim);
	}

	public Boolean getShim() {
		return getBoolean("shim", true);
	}

	public void setTabTip(String tabTip) {
		addOption("tabTip", tabTip);
	}

	public String getTabTip() {
		return getString("tabTip", "");
	}

	public void setTitleCollapse(Boolean titleCollapse) {
		addOption("titleCollapse", titleCollapse);
	}

	public Boolean isTitleCollapse() {
		return getBoolean("titleCollapse", false);
	}

	public void setFrame(Boolean frame) {
		addOption("frame", frame);
	}

	public Boolean isFrame() {
		return getBoolean("frame", true);
	}

	public void unStyled(Boolean unstyled) {
		addOption("unstyled", unstyled);
	}

	public void setFloating(Boolean floating) {
		addOption("floating", floating);
	}

	public Boolean isFloating() {
		return getBoolean("floating", false);
	}
	
	public void setDraggable(Boolean draggable){
		addOption("draggable",draggable);
	}
	
	public Boolean isDraggable(){
		return getBoolean("draggable",false);
	}

	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.Panel(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}
}
